<?php

namespace app\common\model;

use think\Model;
use app\common\traits\model\TraitModel;
use \think\Exception;

/**
 * 会员资金模型
 */
class Capitals extends Model
{
    use TraitModel;

    protected $table = 'deposit';

    public function users()
    {
        return $this->belongsTo('app\user\model\Users', 'user_id');
    }

    public function getCapital($user_id,$field = '')
    {
        $user_capital = $this->get(['user_id' => $user_id]);
        if (!$user_capital) {
            $user_capital = $this->createCapital(['user_id' => $user_id,'amount'=>0]);
        }

        if ($field) {
            if (isset($user_capital->$field)) {
                return $user_capital->$field; 
            }
            return false;
        }
        return $user_capital;
    }
    public function createCapital($user_capital_info)
    {
        return $this->create($user_capital_info, true);
    }
    /**
     * 增加资金
     * @param  [type] $amount  [description]
     * @param  [type] $user_id [description]
     * @param  array  $option  [description]
     * @return [type]          [description]
     */
    public function incCapital($amount, $user_id, $option = [])
    {
        $option = array_merge(['type' => '收入', 'remark' => '收入'], $option);
        $this->capitalOperation($amount, $user_id, $option);
    }
    /**
     * 减少资金
     * @param  [type] $amount  [description]
     * @param  [type] $user_id [description]
     * @param  array  $option  [description]
     * @return [type]          [description]
     */
    public function reduceCapital($amount, $user_id, $option = [], $abs = false)
    {
        $option = array_merge(['type' => '消费', 'remark' => '消费'], $option);
        $this->capitalOperation((0 - abs($amount)), $user_id, $option, $abs);
    }
    /**
     * 资金操作
     * @param  [type] $amount  [description]
     * @param  [type] $user_id [description]
     * @param  array  $option  [description]
     * @param  [type] $abs     [description]
     * @return [type]          [description]
     */
    protected function capitalOperation($amount, $user_id, $option = [], $abs = false)
    {
        $user_capital = $this->getCapital($user_id);
        if ((int)$amount < 0 && $abs === true) {
            if ($user_capital->amount < abs($amount)) {
                throw new Exception("Deficiency of capital",-89);return false;
            }
        }

        $user_capital->amount = $user_capital->amount + $amount;
        $user_capital->save();
        $user_info = $user_capital->users;
        //营地信息
        $camp_name = db('users')->where('id',$user_id)->value('camp_name');
        $camp_id   = db('users')->where('id',$user_id)->value('camp_id');
        $branch_id = db('camps')->where('id',$camp_id)->value('branch_id');
        $branch_name = db('camps')->where('id',$camp_id)->value('branch_name');
        //现金流水补充
        $logs = [
            'user_id'   => $user_id, 
            'branch_id' => $branch_id,
            'branch_name' => $branch_name,
            'cash'      => abs($amount), 
            'type'      => $option['type'], 
            'remark'    => $option['remark'],
            'camp_id'   => $user_info->camp_id,
            'camp_name' => $camp_name 
        ];
        $this->createCapitalLogs($logs);
    }
    //流水,写入日志
    private function createCapitalLogs($capital_info)
    {
        return model('CashDetail')->create($capital_info, true);
    }
}
